package edu.pku.sei.act.sampletask.parkinglot;

import java.util.ArrayList;

import edu.pku.sei.act.actools.IEnvEntry;
import edu.pku.sei.act.actools.util.EnvInfo;
import edu.pku.sei.act.actools.util.TaskMessage;
import edu.pku.sei.act.sampletask.AutonomicParking;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;

public class _Working extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		IEnvEntry iee = AutonomicParking.getEe();
		TaskMessage msg = _Parkinglot.communication.PARKING_SLOT_NOTIFICATION.clone();
		ArrayList<CharSequence> result = intent.getCharSequenceArrayListExtra("result");
		if (result == null){
			Log.e("_Working","result is null");
		}
		if (result.size() != 1){
			Log.w("_Working","result size is :"+result.size());
		} else {
			String s = result.get(0).toString();
			
			TaskMessage msgGot = new TaskMessage(s);
			msg.setReceiver(msgGot.getSender());
		}
		try {
			String t = AutonomicParking.getCm().invokeCapability(_Parkinglot.capability._PROVIDE_POSITION,null);
			Log.i("_Working","invokeCapability:" + t);
			msg.setContent(t);
			msg.setSender(iee.getIdentity());
			iee.notifyRWrite(EnvInfo.HEAD_TUPLE + "," + msg, 0, -1);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
